package netty;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tantexian on 2017/6/12.
 */
@Path("user")
public class UserDemo {
    private static Map<String, User> UserMap = new HashMap<String, User>();

    static {
        User user = new User();
        user.setName("defaultName");
        user.setPassword("withoutPassword");
        UserMap.put(user.getName(), user);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public User get() {
        return UserMap.get("defaultName");
    }

    @Path("/{name}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public User getByName(@PathParam("name") String name) {
        System.out.println("getByName: " + name);
        System.out.println("UserMap:" + UserMap);
        return UserMap.get(name);
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public User create(User user) {
        System.out.println("user: " + user);
        if (UserMap.containsKey(user.getName())) {
            return null;
        }
        UserMap.put(user.getName(), user);
        return UserMap.get(user.getName());
    }

    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    public User update(User user) {
        System.out.println("user: " + user);
        if (!UserMap.containsKey(user.getName())) {
            return null;
        }
        UserMap.put(user.getName(), user);
        return UserMap.get(user.getName());
    }

    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    public User del(User user) {
        System.out.println("user: " + user);
        UserMap.remove(user.getName());
        if (!UserMap.containsKey(user.getName())) {
            return null;
        }
        return user;
    }

    @Path("/testHead")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public String head(@HeaderParam("authkey") String authKey) {
        System.out.println("authKey: " + authKey);
        return authKey;
    }
}
